package Student.Manage;

import java.util.ArrayList;
import java.util.Scanner;

public class Manage {
    public static void main(String[] args) {
        ArrayList<Student> Array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        start:
        while (true) {
            System.out.println("---------欢迎来到学生管理系统---------");
            System.out.println("请回复数字进行以下操作：");
            System.out.println("1:添加学生信息；");
            System.out.println("2:修改学生信息；");
            System.out.println("3:查找学生信息；");
            System.out.println("4:所有学生信息；");
            System.out.println("5:删除学生信息；");
            System.out.println("6:退出管理系统；");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    addPlus(Array);
                    System.out.println("添加成功");
                    break;
                case 2:
                    if (Array.size() == 0) {
                        System.out.println("系统没有学号，请输入后再进行其他操作;");
                        break;
                    } else {
                        setPlus(Array);
                        System.out.println("修改成功");
                        break;
                    }
                case 3:
                    if (Array.size() == 0) {
                        System.out.println("系统没有学号，请输入后再进行其他操作;");
                        break;
                    } else {
                        findPlus(Array);
                        System.out.println("查找成功");
                        break;
                    }
                case 4:
                    if (Array.size() == 0) {
                        System.out.println("系统没有学号，请输入后再进行其他操作;");
                        break;
                    } else {
                        System.out.println("学生信息如下：");
                        AllPlus(Array);
                        break;
                    }
                case 5:
                    if (Array.size() == 0) {
                        System.out.println("系统没有学号，请输入后再进行其他操作;");
                        break;
                    } else {
                        deletePlus(Array);
                        System.out.println("删除成功");
                        break;
                    }
                case 6:
                    System.out.println("感谢您的使用；再见！");
                    break start;
                default:
                    System.out.println("输入有误，请重新输入：");
                    break;
            }
        }
    }

    public static ArrayList<Student> addPlus(ArrayList<Student> Array) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号：");
        String number = sc.nextLine();
        stu.setNumber(number);
        for (int j = 0; j < Array.size(); j++) {
            if (Array.get(j).getNumber().equals(number)) {
                System.out.println("输入学号已存在，请重新输入学号：");
                number = sc.nextLine();
                stu.setNumber(number);
                j = 0;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        stu.setName(name);
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生分数：");
        int score = sc.nextInt();
        stu.setScore(score);
        Array.add(stu);

        return Array;
    }

    public static ArrayList<Student> setPlus(ArrayList<Student> Array) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        set1:
        for (int j = 0; j < Array.size(); j++) {
            System.out.println("请输入需要修改的学号");
            String number = sc.nextLine();
            for (int i = 0; i < Array.size(); i++) {
                if (number.equals(Array.get(i).getNumber())) {
                    System.out.println("请输入需要修改的姓名：");
                    String name = sc.nextLine();
                    Array.get(i).setName(name);
                    System.out.println("请输入需要修改的年龄：");
                    int age = sc.nextInt();
                    Array.get(i).setAge(age);
                    System.out.println("请输入需要修改的成绩：");
                    int score = sc.nextInt();
                    Array.get(i).setScore(score);
                    break set1;
                }
            }
            System.out.println("学号输入有误，请重新输入：");
            j--;
        }
        return Array;
    }

    public static ArrayList<Student> findPlus(ArrayList<Student> Array) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        find:
        for (int j = 0; j < Array.size(); j++) {
            System.out.println("请输入你要查找的学号：");
            String number = sc.nextLine();
            for (int i = 0; i < Array.size(); i++) {
                if (number.equals(Array.get(i).getNumber())) {
                    System.out.println("学号" + "    " + "姓名" + "    " + "年龄" + "    " + "成绩");
                    System.out.println(Array.get(i).getNumber() + "   " + Array.get(i).getName() + "     " + Array.get(i).getAge() + "     " + Array.get(i).getScore());
                    break find;
                }
            }
            System.out.println("学号输入有误，请重新输入：");
            j--;
        }
        return Array;
    }

    public static void AllPlus(ArrayList<Student> Array) {
        System.out.println("学号" + "    " + "姓名" + "    " + "年龄" + "    " + "成绩");
        for (int i = 0; i < Array.size(); i++) {
            System.out.println(Array.get(i).getNumber() + "   " + Array.get(i).getName() + "     " + Array.get(i).getAge() + "     " + Array.get(i).getScore());
        }
    }

    public static ArrayList<Student> deletePlus(ArrayList<Student> Array) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        dele:
        for (int j = 0; j < Array.size(); j++) {
            System.out.println("请输入你要删除的学号：");
            String number = sc.nextLine();
            for (int i = 0; i < Array.size(); i++) {
                if (number.equals(Array.get(i).getNumber())) {
                    Array.remove(i);
                    break dele;
                }
            }
            System.out.println("学号输入有误，请重新输入：");
            j--;
        }
        return Array;
    }
}


